<?php

namespace haibao\cms\data\emode;

use haibao\cms\model\filter\activity\Activity as ActivityFilter;
use haibao\cms\web\ajax\Feature;

class EmodeBrand extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\emode\EmodeBrand');
    }
    
    public function getEmodeBrandList($query){
        return $this->getAll($query);
    }
    
    public function saveEmodeBrand($model){
        if($model->Id){
            $this->save($model);
        }else{
            $this->add($model);
        }
       
    }
    
    public function getEmodeBrandById($brandId){
        $sql = 'select Id,Name,ImageId,LinkUrl,Description,ShowTime from EmodeBrand where Id = %s and Status = %s';
        return $this->query($sql,array($brandId,\haibao\cms\model\data\emode\EmodeBrand::STATUS_ENABLE))->fetch_assoc();
    }
    
    public function setBrandStatus($id,$status){
        $sql = 'update EmodeBrand set Status = %s where Id = %s';
        $this->query($sql,array($status,$id));
        
        $sql = 'update EmodeProduct set Status = %s where BrandId = %s';
        $this->query($sql,array($status,$id));
    }
    
    public function getEmodeBrand(){
        $sql = 'select Id,Name,ImageId,Description,LinkUrl from EmodeBrand where Status = %s order by ShowTime desc limit 5';
        $result = $this->query($sql,array(\haibao\cms\model\data\emode\EmodeBrand::STATUS_ENABLE));
        $brandArray = array();
        while($row = $result->fetch_assoc()){
            $brandArray[] = $row;
        }
        return $brandArray;
    }
    
    public function getEmodeProductByBrandId(){
        $sql = 'select a.ProductName,a.ImageId,a.Description,a.ProductPrice,a.ProductLinkUrl,b.Name from EmodeProduct as a left join EmodeBrand as b on a.BrandId=b.Id where a.Status = %s order by a.ShowTime desc limit 12';
        $productResult = $this->query($sql,array(\haibao\cms\model\data\emode\EmodeProduct::STATUS_ENABLE));
        $productArr = array();
        while($row = $productResult->fetch_assoc()){
            $productArr[] = $row;
        }
        return $productArr;
    }
    
}